package com.nowcoder;

import com.nowcoder.dao.DiscussPostMapper;
import com.nowcoder.entity.DiscussPost;
import com.nowcoder.controller.es.DiscussPostRepository;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.*;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest
public class ESText {
    @Autowired
    DiscussPostMapper mapper;

    @Autowired
    DiscussPostRepository repository;

    @Autowired
    private ElasticsearchRestTemplate template;

    @Test
    public void testtest() {
        System.out.println(repository);
    }

    @Test
    public void testInsert() {
        repository.save(mapper.selectDiscussPostById(241));
        repository.save(mapper.selectDiscussPostById(242));
        repository.save(mapper.selectDiscussPostById(243));
    }

    @Test
    public void testInsertAll() {
        repository.saveAll(mapper.selectDiscussPosts(100, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(102, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(103, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(111, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(112, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(131, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(132, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(133, 0, 100, 0));
        repository.saveAll(mapper.selectDiscussPosts(134, 0, 100, 0));
    }

    @Test
    public void testUpdate(){
        DiscussPost post = mapper.selectDiscussPostById(231);
        post.setContent("好好学习");
        repository.save(post);
    }

    @Test
    public void testDelete(){
        repository.deleteById(231);
        // 删除所有数据，慎用
        // repository.deleteAll();
    }

    /* 在title、content字段中搜索互联网寒冬
        然后按照type、score、createTime进行排序
        设置分页条件
        增加高亮添加
     */
    @Test
    public void testSearchByRepository() {
        NativeSearchQuery build = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))
                .withSorts(SortBuilders.fieldSort("type").order(SortOrder.DESC))
                .withSorts(SortBuilders.fieldSort("score").order(SortOrder.DESC))
                .withSorts(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .withPageable(PageRequest.of(0, 10))
                .withHighlightFields(
                        new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),
                        new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")
                ).build();

        // 搜索
        SearchHits<DiscussPost> search = template.search(build, DiscussPost.class);
        // 构造分页
        SearchPage<DiscussPost> page = SearchHitSupport.searchPageFor(search, build.getPageable());

        // 全部的元素
        System.out.println(page.getTotalElements());
        // 全部页数
        System.out.println(page.getTotalPages());
        // 每一页大小
        System.out.println(page.getSize());
        for (SearchHit<DiscussPost> discussPostSearchHit : page) {
            System.out.println(discussPostSearchHit.getContent()); //原始内容
            System.out.println(discussPostSearchHit.getHighlightFields()); //高亮内容
        }
    }
}




















